package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("获取到购物车数据:{}", shoppingCart);
        //先获取用户id设置的表中
        Long userID = BaseContext.getId();
        shoppingCart.setUserId(userID);
        //查询构造器
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //设置查询条件
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userID);
        Long dishId = shoppingCart.getDishId();
        //查看表中是否有对应的菜品或者套餐
        if (dishId!=null){
            //添加到购物车的是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart shoppingCartServiceOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (shoppingCartServiceOne!=null){
            //如果不等于null就在原先数量上+1
            Integer number = shoppingCartServiceOne.getNumber();
            shoppingCartServiceOne.setNumber(number+1);
            shoppingCartServiceOne.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(shoppingCartServiceOne);
        }else{
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            shoppingCartServiceOne=shoppingCart;
        }

        return R.success(shoppingCartServiceOne);
    }


    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        //根据当前用户id来查询购物车中是否有数据
        Long userID = BaseContext.getId();
        //构造查询条件
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userID);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        return R.success(shoppingCartList);
    }

    @DeleteMapping("/clean")
    public R<String> clean(){
        //根据用户id获取购物车中的菜品数据,如果有就删除
        Long userID = BaseContext.getId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userID);
        shoppingCartService.remove(lqw);
       /* List<ShoppingCart> list = shoppingCartService.list(lqw);
        for (ShoppingCart shoppingCart : list) {
            shoppingCartService.removeById(shoppingCart.getUserId());
        }*/
        return R.success("删除成功```");
    }
}
